/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.system;

import java.util.List;
import javax.faces.application.FacesMessage;
import src.services.system.CompanyModel;
import src.domain.system.Company;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import src.domain.gl.Currency;
import src.services.gl.CurrencyModel;
import src.util.Messager;

/**
 *
 * @author jay
 */
@ManagedBean
@RequestScoped
public class CompanyBean {

    private Company company;
    private List<Currency> avaibleCurrency;
    private Messager messager;
    private boolean editMode;

    public CompanyBean() {
        messager = new Messager(); 
        CompanyModel helper = new CompanyModel();
    
        this.company = helper.getCompany();
        if (this.company==null) 
            this.company = new Company();
        
        CurrencyModel curr = new CurrencyModel();
        this.avaibleCurrency = curr.findAll(null);
        
    }

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }
    
    public boolean isEditMode() {
        return editMode;
    }
    
    public List<Currency> getAvaibleCurrency() {
        return avaibleCurrency;
    }
    
    public void handleClose(CloseEvent event) {
    //    this.selectedData = null;
      //  this.insertedData = null;
        //this.editMode = true;      
    }
    
    public void newData() {  
        //this.selectedData = null;
        //this.editMode = false;
        //this.insertedData = new Company();
    
       // FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "New Data");  
      //  FacesContext.getCurrentInstance().addMessage(null, msg); 
    }
    
    public void save() {
                
        RequestContext context = RequestContext.getCurrentInstance();  
        FacesMessage msg = null;
        boolean success = true;  
            
        CompanyModel helper = new CompanyModel();
               
        success = helper.saveData(this.company);
        if (success) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "Penyimpanan berhasil.");
            
            //if (this.editMode!=true)
              //  this.companys.add(this.insertedData);
            
        } else {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "Penyimpanan tidak berhasil.");  
        }
         
        //msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info: ", this.company.getCompanyName()); 
        FacesContext.getCurrentInstance().addMessage(null, msg);  
        context.addCallbackParam("success", success);  
        
    }
    
    public StringBuffer loadCompanyInfo(){
        StringBuffer rs = new StringBuffer();
        if (this.company!=null){
            rs.append(this.company.getCompanyName());
            rs.append("\n");
            rs.append(this.company.getAddressLine1());
            rs.append("\n");
            rs.append("Telp. ").append(company.getPhone()).append("  Fax. ").append(company.getFax());
            rs.append("\n");
            rs.append("Email. ").append((company.getEmail()==null?"-":company.getEmail())).append("  Website. ").append((company.getWebsite()==null?"-":company.getWebsite()));            
        }
        return rs;
    }
    
    public void delete() {
      /*  
        RequestContext context = RequestContext.getCurrentInstance();  
        FacesMessage msg = null;
        boolean success = true;  
       // msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info: ", this.selectedData.getCompanyName()); 
        
        CompanyModel helper = new CompanyModel();
        success = helper.deleteData(this.selectedData);
        
        if (success) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "Penghapusan berhasil.");
           
            this.companys.remove(this.selectedData);
            //this.selectedData = null;
        } else {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "Penghapusan tidak berhasil.");  
        }
               
        FacesContext.getCurrentInstance().addMessage(null, msg);  
        context.addCallbackParam("success", success);  
       * 
       */
    }
    
}
